package com.alex.blog.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class MyShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(defaultWebSecurityManager());

        // 添加shiro内置过滤器
        /**
            anon: 无需认证
            authc: 必须认证
            user: 必须拥有记住我功能才能用
            perms: 必须拥有对某个资源的权限才能访问
            role: 必须拥有某个角色
         **/
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
//        map.put("/user/add", "authc");
//        map.put("/user/del", "authc");
        map.put("/user/*", "authc");
        factoryBean.setFilterChainDefinitionMap(map);

        factoryBean.setLoginUrl("/login");

        return factoryBean;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm());
        return securityManager;
    }

    @Bean
    public MyRealm myRealm() {
        return new MyRealm();
    }
}
